package com.cssl.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.cssl.domain.*;
import com.cssl.domain.dto.DtsBrandDto;
import com.cssl.domain.dto.GoodsDto;
import com.cssl.domain.dto.GoodsSearchDto;
import com.cssl.domain.dto.PageDto;
import com.cssl.service.*;
import com.cssl.util.InfoCode;
import com.cssl.util.Result;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
@Api(value = "商品接口", tags = "商品相关操作")
public class GoodsController {
    @Autowired
    private DtsGoodsService service;
    @Resource
    private DtsCouponService dtsCouponService;
//    @Resource
//    private GroupOnService groupOnService;
    @Resource
    private DtsBrandService dtsBrandService;
    @Resource
    private DtsCategoryService categoryService;
    @Resource
    private DtsTopicService dtsTopicService;

    @PostMapping("/firstPageGoods")
    @ApiOperation(value = "展示首页数据", notes = "传入pageIndex,pageSize")
    public Result showFirstPageGoods(@RequestBody com.cssl.domain.Page page) {
        Page pages = service.findFirstPageGoods(page);
        Map map = new HashMap();
        map.put("list", pages.getResult());
        map.put("total", pages.getTotal());
        map.put("totalPage", pages.getPages());
        if (ObjectUtil.isEmpty(pages)) {
            return Result.result(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, null, System.currentTimeMillis());
        }
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, map, System.currentTimeMillis());
    }


    /**
     * (根据浏览量和销售量判断)
     *
     * @return
     */
    @PostMapping("/prodList")
    @ApiOperation(value = "展示前两条热数据")
    public Result getTwoHotGoods() {
        List<DtsGoods> list = service.getTwoHotGoods();
        if (ObjectUtil.isEmpty(list)) {
            return Result.result(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, null, System.currentTimeMillis());
        }
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, list, System.currentTimeMillis());
    }


    /**
     * 获取商品列表
     *
     * @param goodsDto 查询条件
     * @return
     */
    @PostMapping("/goodsList")
    @ApiOperation(value = "获取商品列表", notes = "传入goodsDto")
    public Result getGoodsList(@RequestBody GoodsDto goodsDto) {
        Map map = service.getGoodsList(goodsDto);
        if (ObjectUtil.isEmpty(map.get("list"))) {
            return Result.result(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, null, System.currentTimeMillis());
        }
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, map, System.currentTimeMillis());
    }

    /**
     * 获得商品总数
     *
     * @return
     */
    @PostMapping("/goodsCount")
    @ApiOperation(value = "获得商品总数")
    public Result getTotalGoodsCount() {
        return service.getTotalGoodsCount();
    }

    @PostMapping("/firstPageInfo")
    @ApiOperation(value = "获得商城首页数据")
    public Result getFirstPage() {
        List<DtsGoods> hotGoods = service.getHotGoods();// 火热
        List<DtsGoods> newGoods = service.getNewGoods();// 新
        List<DtsAd> noticeList = service.findNoticeList();
        List<DtsCoupon> allInfo = dtsCouponService.getAllInfo();//优惠券
//        List<Map> active = groupOnService.getActive();//团购
        List<DtsBrand> allBrand = dtsBrandService.getAllBrand();//品牌
        List<DtsTopic> allTopic= dtsTopicService.getTopicList();//专题
        // 查询一级分类
        List<DtsCategory> dtsCategories = categoryService.getCategory("L1", 0);// 一级分类
        Result notices = service.getNotice(0);
        List<DtsArticle> notice = (List<DtsArticle>) notices.getData();// 公告

        Map<String,Object> maps=new HashMap<>();
        maps.put("hotGoods",hotGoods);
        maps.put("newGoods",newGoods);
        maps.put("noticeList",noticeList);
        maps.put("allInfo",allInfo);
//        maps.put("active",active);
        maps.put("allBrand",allBrand);
        maps.put("dtsCategories",dtsCategories);
        maps.put("notice",notice);
        maps.put("allTopic",allTopic);
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, maps, System.currentTimeMillis());
    }

    /**
     * @param map id,公告id
     * @return
     */
    @PostMapping("/notice")
    @ApiOperation(value = "公告")
    public Result getNotice(@RequestBody Map map) {
        Integer id = Integer.parseInt(map.get("id").toString());
        return service.getNotice(id);
    }

    /**
     * 查询三级商品分类
     *
     * @param map categoryId page size
     * @return totalPages ,  goodsList
     */
    @PostMapping("/getLevel3")
    @ApiOperation(value = "三级分类", notes = "二级id,page,size")
    public Result getLevel3(@RequestBody Map<String, Object> map) {
        Integer pages = Integer.parseInt(map.get("page").toString());
        Integer size = Integer.parseInt(map.get("size").toString());
        Integer categoryId = Integer.parseInt(map.get("categoryId").toString());

        Page page = categoryService.getLevel3(pages, size, categoryId);
        Map pagehelper = new HashMap();
        pagehelper.put("totalPages", page.getPages());
        pagehelper.put("goodsList", page.getResult());
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, pagehelper, System.currentTimeMillis());
    }

    /**
     * 根据id获得商品所有的信息
     *
     * @param map id
     * @return "baseInfo","attribute" ,"comment","specifications","product","brand"
     * controller层判断用户是否登录
     */
    @PostMapping("/getGoodsInfoById")
    @ApiOperation(value = "根据id获得商品所有的信息", notes = "id")
    public Result getGoodsInfoById(@RequestBody Map map) {
        Object id = map.get("id");
        if (ObjectUtil.isEmpty(id)) {
            return Result.fail(InfoCode.BAD_REQUEST, InfoCode.MESSAGE_BAD_REQUEST, System.currentTimeMillis());
        }
        return service.getGoodsInfoById(Integer.parseInt(id.toString()));
    }


    /**
     * 搜索关键字
     *
     * @return hotKeyWordList ,  goodsKeyWordList
     */
    @PostMapping("/searchHelp")
    @ApiOperation(value = "搜索历史,热门词,关键字", notes = "二级id,page,size")
    public Result searchHelp() {
        return service.searchHelp();
    }

    /**
     * 模糊查询商品
     *
     * @param searchDto
     * @return goodsList , page , categoryName
     */
    @PostMapping("/searchGoods")
    @ApiOperation(value = "模糊查询你商品")
    public Result searchGoods(@RequestBody GoodsSearchDto searchDto) {
        return service.searchGoods(searchDto);
    }

    /**
     * 具体商品的规格
     *
     * @param map 商品id goodsId
     * @return
     */
    @PostMapping("/goodsSpecifications")
    @ApiOperation(value = "具体商品的规格", notes = "商品Id:goodsId")
    public Result goodsSpecifications(@RequestBody Map map) {
        return service.getGoodsSpecifications(map);
    }

    @PostMapping("/relevantData")
    @ApiOperation(value = "获取规格相对应的参数", notes = "商品id,选中的值")
    public Result relevantData(@RequestBody Map map) {
        return service.getRelevantData(map);
    }

    @PostMapping("/getBrandById")
    @ApiOperation(value = "根据id获得品牌商", notes = "品牌商id")
    public Result getBrandById(@RequestBody Map map) {
        return dtsBrandService.getBrandById(map);
    }

    @PostMapping("/getGoodsByBrandId")
    @ApiOperation(value = "根据品牌商id获得商品", notes = "DtsBrandDto")
    public Result getGoodsByBrandId(@RequestBody DtsBrandDto dto) {
        return service.getGoodsByBrandId(dto);
    }

    @PostMapping("/getHotOrNewGoods")
    @ApiOperation(value = "获得热商品或最新商品", notes = "PageDto")
    public Result getHotAndNewGoods(@RequestBody PageDto dto) {
        return service.getHotAndNewGoods(dto);
    }

    @PostMapping("/getAllBrand")
    @ApiOperation(value = "分页品牌商", notes = "接收:page,size")
    public Result getAllBrand(@RequestBody Map map) {
        Map maps = dtsBrandService.getAllBrandPage(map);
        if (maps != null) {
            return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, maps, System.currentTimeMillis());
        }
        return Result.fail(InfoCode.QUERY_EMPTY, InfoCode.MESSAGE_QUERY_EMPTY, System.currentTimeMillis());
    }

    @PostMapping("/checkedSpecPrice")
    @ApiOperation(value = "选中规格的价格", notes = "id,规格")
    public Result checkedSpecPrice(@RequestBody Map map) {
        return service.checkedSpecPrice(map);
    }

    @PostMapping("/goodsOnShelf")
    @ApiOperation(value = "上架新商品模块", notes = "商品基本信息，商品属性，商品规格，商品货品")
    public Result goodsOnShelf(@RequestBody Map map) {
        Boolean flag = false;
        if (service.insertGoods(map) > 0) {
            service.updateGoodsSn();
            map.put("id", service.getNewGoodsSn());
            service.insertGoodsAttribute(map);
            service.insertGoodsSpecification(map);
            service.insertGoodsProduct(map);
            flag = true;
        }
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, flag, System.currentTimeMillis());
    }


    @PostMapping("/getGoodsRelated")
    @ApiOperation(value = "大家都在看商品", notes = "接收商品id")
    public Result getGoodsRelated(@RequestBody DtsGoods goods) {
        return service.getGoodsRelated(goods);
    }

    @PostMapping("/getGoodsAllInfoById")
    @ApiOperation(value = "根据id获得商品所有的信息", notes = "商品id")
    public Result getGoodsAllInfoById(@RequestBody Map map) {
        Map goodsInfo = new HashMap();
        goodsInfo.put("goods", service.getDtsGoodsById(Integer.valueOf(map.get("id").toString())));
        goodsInfo.put("attributes", service.getGoodsAttributeByGoodsId(Integer.valueOf(map.get("id").toString())));
        goodsInfo.put("specifications", service.getGoodsSpecificationByGoodsId(Integer.valueOf(map.get("id").toString())));
        goodsInfo.put("products", service.getGoodsProductByGoodsId(Integer.valueOf(map.get("id").toString())));
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, goodsInfo, System.currentTimeMillis());
    }

    @PostMapping("/updateGoodsAllInfoById")
    @ApiOperation(value = "根据id修改商品所有的信息", notes = "商品基本信息，商品属性，商品规格，商品货品")
    public Result updateGoodsAllInfoById(@RequestBody Map map) {
        Boolean flag = false;
        service.deleteGoodsAttributeByGoodsId(Integer.valueOf(map.get("id").toString()));
        service.deleteGoodsSpecificationByGoodsId(Integer.valueOf(map.get("id").toString()));
        service.deleteGoodsProductByGoodsId(Integer.valueOf(map.get("id").toString()));
        DtsGoods dtsGoods = new DtsGoods();
        BeanUtil.copyProperties(map.get("goods"), dtsGoods);
        dtsGoods.setUpdateTime(new Date());
        if (service.updateGoods(dtsGoods) > 0) {
            flag = true;
            service.insertGoodsAttribute(map);
            service.insertGoodsSpecification(map);
            service.insertGoodsProduct(map);
        }
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, flag, System.currentTimeMillis());
    }

    @PostMapping("/deleteGoodsById")
    @ApiOperation(value = "根据id删除商品所有的信息 ", notes = "商品id")
    public Result deleteGoodsById(@RequestBody Map map) {
        Integer id = Integer.valueOf(map.get("id").toString());
        service.deleteGoodsAttributeByGoodsId(id);
        service.deleteGoodsSpecificationByGoodsId(id);
        service.deleteGoodsProductByGoodsId(id);
        Boolean flag = service.deleteGoods(id) > 0;
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, flag, System.currentTimeMillis());
    }

    @PostMapping("/goodsInOrder")
    @ApiOperation(value = "根据id删除商品所有的信息", notes = "商品id")
    public Result goodsInOrder(@RequestBody Integer goodsId) {
        Boolean flag = service.goodsInOrder(goodsId) > 0;
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, flag, System.currentTimeMillis());
    }

    @PostMapping("/groupOnShow")
    @ApiOperation(value = "根据商品id展示当前的团购", notes = "商品id")
    public Result groupOnShow(@RequestBody DtsGoods goods){
        return service.groupOnShow(goods.getId());
    }
}
